<?php echo $header; ?>
<div class="box">
    <div class="content">
        <h2 class="h2title">User</h2>
        <div>
            <div class="warning" style="margin-top:10px;display:none;">
            </div>
            <table style="padding:10px;" class="tbluserentry">
                <tr>
                    <td align="right">
                        <span class="spanlablebold" style="margin-right:5px;">First Name</span>
                    </td>
                    <td>
                        <input type="text" class="txt" id="txtfname" name="txtfname" maxlength="50"/>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        <span class="spanlablebold" style="margin-right:5px;">Last Name</span>
                    </td>
                    <td>
                        <input type="text" class="txt" id="txtlname" name="txtlname" maxlength="50"/>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        <span class="spanlablebold" style="margin-right:5px;">User Name</span>
                    </td>
                    <td>
                        <input type="text" class="txt" id="txtuname" name="txtuname"/>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        <span class="spanlablebold" style="margin-right:5px;">Password</span>
                    </td>
                    <td>
                        <input type="password" class="txt" id="txtpword" name="txtpword"/>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        <span class="spanlablebold" style="margin-right:5px;">Active </span>
                    </td>
                    <td>
                        <input type="radio" name="rdbactive" value="Y" checked="true"/><span class="spanlablebold" style="margin-right:10px;">Yes</span>
                        <input type="radio" name="rdbactive" value="N"/><span class="spanlablebold">No</span>
                    </td>
                </tr>
            </table>
        </div>
    </div>
</div>
<div class="buttonbar">
    <input type="hidden" id="hiduserid" name="hiduserid" value=""/><input type="button" value="Save" id="btnsave" name="btnsave" class="btnradius" style="margin-right:10px;"/><input type="button" value="Reset" id="btnreset" name="btnreset" class="btnradius"/>
</div>
<div class="box" style="margin-top:10px;">
    <div class="content">
        <table id="tblusers" class="list">
            <thead>
                <td class="left" style="width:15%">
                    First Name
                </td>
                <td class="left" style="width:15%">
                    Last Name
                </td>
                <td class="left" style="width:20%">
                    User Name
                </td>
                <td class="left" style="width:20%">
                    Password
                </td>
                <td class="left" style="width:10%">
                    Active Y/N
                </td>
                <td style="width:10%">
                </td>
                <td style="width:10%">
                </td>
            </thead>
            <tbody>
                <?php foreach($user_list as $row){ ?>
                <tr userid="<?php echo $row['user_id']; ?>">
                    <td class="left">
                        <?php echo $row["firstname"]; ?>
                    </td>
                    <td class="left">
                        <?php echo $row["lastname"]; ?>
                    </td>
                    <td class="left">
                        <?php echo $row["username"]; ?>
                    </td>
                    <td class="left">
                        <?php echo $row["password"]; ?>
                    </td>
                    <td class="left">
                        <?php echo $row["active_yn"]; ?>
                    </td>
                    <td class="center">
                        <img class="imgedit" src="<?php echo DIR_IMAGES.'edit.png'; ?>" style="width:20px;height:20px;cursor:pointer;"/>
                    </td>
                    <td class="center">
                        <img class="imgdel" src="<?php echo DIR_IMAGES.'delete.png'; ?>" style="width:20px;height:20px;cursor:pointer;"/>
                    </td>
                </tr>
                <?php } ?>
            </tbody>
        </table>
    </div>
</div>
<script type="text/javascript">
    
    $(".imgedit").live("click", function(){
        $(".warning").hide();
        var ele = $(this).closest("tr"), suserid = ele.attr("userid");
        if (suserid) {
            loading("show");
            $.ajax({
                url: "<?php echo MAIN_PAGE.'usermanagement/edit';?>",
                type: 'POST',
                data: {
                    userid: suserid
                },
                dataType: 'json',
                success: function(res){
					//alert(res);
                    loading('hide');
                    if (res.status) {
                        $("#hiduserid").val(res.user_id);
                        $("#txtfname").val(res.firstname);
                        $("#txtlname").val(res.lastname);
                        $("#txtuname").val(res.username);
                        $("#txtpword").val(res.password);
                        if (res.active_yn == "Y") {
                            $("input[name='rdbactive']:eq(0)").attr("checked", true);
                        }
                        else {
                            $("input[name='rdbactive']:eq(1)").attr("checked", true);
                        }
                    }
                    else {
                        alert(res.msg);
                    }
                }
            });
        }
    });
    
    $(".imgdel").live("click", function(){
        $(".warning").hide();
		$("#btnreset").trigger("click");
        var ele = $(this).closest("tr"), suserid = ele.attr("userid");
        if (suserid) {
            loading("show");
            $.ajax({
                url: "<?php echo MAIN_PAGE.'usermanagement/delete';?>",
                type: 'POST',
                data: {
                    userid: suserid
                },
                dataType: 'json',
                success: function(res){
                    loading('hide');
                    if (res.status) {
                        ele.animate({
                            backgroundColor: "#f21212"
                        }, "fast").animate({
                            opacity: "hide"
                        }, "slow", function(){
                            ele.remove();
                        });
                    }
                    else {
                        alert(res.msg);
                    }
                }
            });
        }
    });
    
    $("#btnsave").click(function(){
        if (validate()) {
            loading("show");
            $.ajax({
                url: "<?php echo MAIN_PAGE.'usermanagement/userpost';?>",
                type: 'POST',
                data: {
                    userid: $("#hiduserid").val(),
                    fname: $("#txtfname").val(),
                    lname: $("#txtlname").val(),
                    uname: $("#txtuname").val(),
                    pword: $("#txtpword").val(),
                    active: $("input[name='rdbactive']:checked").val()
                },
                dataType: 'json',
                success: function(res){
                    loading('hide');
                    if (res.status) {
                        if ($("#hiduserid").val() == "") {
                            var imgeditpath = '<?php echo DIR_IMAGES."edit.png"; ?>', imgdelpath = '<?php echo DIR_IMAGES."delete.png"; ?>';
                            //$("#btnreset").trigger("click");
                            var str = "<tr userid='" + res.user_id + "'>" +
                            "<td class='left'>" +
                            $("#txtfname").val() +
                            "</td>" +
                            "<td class='left'>" +
                            $("#txtlname").val() +
                            "</td>" +
                            "<td class='left'>" +
                            $("#txtuname").val() +
                            "</td>" +
                            "<td class='left'>" +
                            $("#txtpword").val() +
                            "</td>" +
                            "<td class='left'>" +
                            $("input[name='rdbactive']:checked").val() +
                            "</td>" +
                            "<td class='center'>" +
                            "<img class='imgedit'  src='" +
                            imgeditpath +
                            "' style='width:20px;height:20px;cursor:pointer;'/>" +
                            "</td>" +
                            "<td class='center'>" +
                            "<img class='imgdel' src='" +
                            imgdelpath +
                            "' style='width:20px;height:20px;cursor:pointer;'/>" +
                            "</td>" +
                            "<tr>";							
                            $("#tblusers").append(str);
							$("#hiduserid").val(res.user_id);
							  alert(res.msg);
                        }
                        else {
                            $("#tblusers tbody tr[userid='" + $("#hiduserid").val() + "'] td:eq(0)").text($("#txtfname").val());
                            $("#tblusers tbody tr[userid='" + $("#hiduserid").val() + "'] td:eq(1)").text($("#txtlname").val());
                            $("#tblusers tbody tr[userid='" + $("#hiduserid").val() + "'] td:eq(2)").text($("#txtuname").val());
                            $("#tblusers tbody tr[userid='" + $("#hiduserid").val() + "'] td:eq(3)").text($("#txtpword").val());
                            $("#tblusers tbody tr[userid='" + $("#hiduserid").val() + "'] td:eq(4)").text($("input[name='rdbactive']:checked").val());
							  alert(res.msg);
                        }
                    }
                    else {
                        alert(res.msg);
                    }
                }
            });
        }
    });
    
    $("#btnreset").click(function(){
        $("#txtfname,#txtlname,#txtuname,#txtpword").val('');
        $("#hiduserid").val('');
        $("input[name='rdbactive']:eq(0)").attr("checked", true);
    });
    
    function validate(){
        $(".warning").hide();
        if ($("#txtfname").val() == "") {
            $("#txtfname").focus();
            $(".warning").text("Please enter firstname").show();
            return false;
        }
        if ($("#txtlname").val() == "") {
            $("#txtlname").focus();
            $(".warning").text("Please enter lastname").show();
            return false;
        }
        if ($("#txtuname").val() == "") {
            $("#txtuname").focus();
            $(".warning").text("Please enter username").show();
            return false;
        }
        if ($("#txtpword").val() == "") {
            $("#txtpword").focus();
            $(".warning").text("Please enter username").show();
            return false;
        }
        if ($("input[name='rdbactive']:checked").length == 0) {
            $(".warning").text("Please choose active y/n").show();
            return false;
        }
        return true;
    }
</script>
<?php echo $footer;?>
